package org.stvd.service.admin.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.stvd.entities.admin.Menus;
import org.stvd.entities.admin.TreeNode;
import org.stvd.repository.admin.MenusDao;
import org.stvd.service.admin.MenusService;
import org.stvd.service.base.impl.BaseServiceImpl;

@Service("menusService")
public class MenusServiceImpl extends BaseServiceImpl<Menus> implements MenusService {

    @Resource(name="MenusDao")
    private MenusDao menusDao;
    
    @Override
    public List<Menus> findAll(String guid) {
        return menusDao.findAll(guid);
    }

    @Override
    public List<Menus> findByUid(String userId, String guid) {
        return menusDao.findByUid(userId, guid);
    }
    
    @Override
    public List<Menus> findByRid(List<String> resourcesIds, String guid){
        return menusDao.findByRid(resourcesIds, guid);
    }
    
    @Override
    public List<Menus> findByParentID(String guid, String upperId) {
        return menusDao.findByParentID(guid, upperId);
    }
    
    @Override
    public List<Menus> getAllMenusByParentID(String guid, String upperId) {
        return menusDao.getAllMenusByParentID(guid, upperId);
    }
    
    @Override
    public List<TreeNode> getMenuItems(String guid, String upperId, String userId) {
        String tmpID = "-1" ;
        List<TreeNode> result = new ArrayList<TreeNode>() ;
        if(upperId != null){
            tmpID = upperId ;
        }
        List<Menus> menuList = findByParentID(guid, tmpID, userId);
        for(Menus menu : menuList){
            TreeNode tree = new TreeNode();
            tree.setId(menu.getMenuId());
            tree.setText(menu.getMenuCaption());
            tree.setIconCls(menu.getIcon().equals("")?"fa fa-tag":menu.getIcon());
            tree.setChildren(getMenuItems(guid,menu.getMenuId(), userId));
            tree.getAttributes().put(menu.getMenuId()+"url", menu.getHttpAddress());
            result.add(tree);
        }
        return result ;
    }
    
    @Override
    public List<Menus> findByParentID(String guid,String upperId,String userId){
        List<Menus> list = findByUid(userId, guid);
        List<Menus> lsResult = new ArrayList<Menus>();
        for(Menus menu : list){
            if(menu.getUpperId().equals(upperId)){
                lsResult.add(menu);
            }
        }
        return lsResult;
    }
    
    @Override
    @Cacheable(value="adminCache",key="'MenusService.getMenuTreeList('+#guid+','+#userId+')'")
    public List<TreeNode> getMenuTreeList(String guid, String userId) {
        return getMenuItems(guid, "-1", userId);
    }
    
    @Override
    @Cacheable(value="adminCache",key="'MenusService.getMenuTreeList('+#guid+','+#resourcesIds+')'")
    public List<TreeNode> getMenuTreeListByRole(String guid,List<String> resourcesIds) {
        return getMenuItems(guid, "-1", resourcesIds);
    }
    
    public List<TreeNode> getMenuItems(String guid, String upperId, List<String> resourcesIds) {
        String tmpID = "-1" ;
        List<TreeNode> result = new ArrayList<TreeNode>() ;
        if(upperId != null){
            tmpID = upperId ;
        }
        List<Menus> menuList = findByParentID(guid, tmpID, resourcesIds);
        for(Menus menu : menuList){
            TreeNode tree = new TreeNode();
            tree.setId(menu.getMenuId());
            tree.setText(menu.getMenuCaption());
            tree.setIconCls(menu.getIcon().equals("")?"fa fa-tag":menu.getIcon());
            tree.setChildren(getMenuItems(guid,menu.getMenuId(), resourcesIds));
            tree.getAttributes().put(menu.getMenuId()+"url", menu.getHttpAddress());
            result.add(tree);
        }
        return result ;
    }
    
    public List<Menus> findByParentID(String guid, String upperId, List<String> resourcesIds){
        List<Menus> list = findByRid(resourcesIds, guid);
        List<Menus> lsResult = new ArrayList<Menus>();
        for(Menus menu : list){
            if(menu.getUpperId().equals(upperId)){
                lsResult.add(menu);
            }
        }
        return lsResult;
    }
    
    @Override
    @Transactional
    @CacheEvict(value="adminCache",allEntries=true)
    public void insertMenu(Menus menus) {
        menusDao.insertMenu(menus);
    }

    @Override
    @Transactional 
    @CacheEvict(value="adminCache",allEntries=true)
    public void updateMenu(Menus menus, String upperId) {
        menusDao.updateMenu(menus, upperId);
    }

    @Override
    @Transactional 
    @CacheEvict(value="adminCache",allEntries=true)
    public void updateMenuState(String guid, String upperId) {
        menusDao.updateMenuState(guid, upperId);
    }
    
    @Override
    @Transactional 
    @CacheEvict(value="adminCache",allEntries=true)
    public void deleteMenu(Menus menus) {
        menusDao.deleteMenu(menus);
    }
    
    @Override
    public boolean isChildMenu(String guid, String menuId, String childMenuId){
        boolean bResult = false ;
        List<Menus> list = getAllMenusByParentID(guid, menuId);
        for(Menus menu : list){
            if(menu.getMenuId().equals(childMenuId)){
                bResult = true ;
            }
        }
        return bResult ;
    }
}
